import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:hi_common/hi_common.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:permission_handler/permission_handler.dart';

class Widget2Local {
  static save(GlobalKey globalKey) async {
    BuildContext buildContext = globalKey.currentContext;

    if (await PermissionUtil.checkPermission(Permission.storage)) {
      if (buildContext != null) {
        RenderRepaintBoundary boundary = buildContext.findRenderObject();
        final image = await boundary.toImage(pixelRatio: 3.0);
        ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
        await ImageGallerySaver.saveImage(byteData.buffer.asUint8List(), quality: 100);
        toast('保存成功');
      }
    }
  }
}
